{- This file is part of funbot.
 -
 - Written in 2015, 2016 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

-- | Visit, join, leave commands
--
-- Manage bot channels
module FunBot.Commands.Channels
    ( cmdVisit
    , cmdJoin
    , cmdLeave
    )
where

import Control.Monad (unless, when)
import Data.List (find, intercalate)
import Data.Monoid ((<>))
import Data.Settings.Types (showOption)
import Data.Text (Text)
import FunBot.History (quote, reportHistory')
import FunBot.Memos (submitMemo)
import FunBot.Settings
import FunBot.Settings.Sections.Channels (addChannel)
import FunBot.Settings.Sections.Feeds (addFeed, deleteFeed)
import FunBot.Settings.Sections.Repos
import FunBot.Settings.Sections.Shortcuts (addShortcut, deleteShortcut)
import FunBot.Types
import FunBot.UserOptions
import FunBot.Util (getHistoryLines, cmds, helps)
import Network.IRC.Fun.Bot.Behavior
import Network.IRC.Fun.Bot.Chat
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Bot.Types
import Text.Read (readMaybe)
import Network.IRC.Fun.Types.Base

import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.Read as TR

respondVisit
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondVisit _mchan _nick [chan] send = do
    memb <- botMemberOf $ Channel chan
    send $ MsgContent $ if memb
        then "I’m already a member of that channel. I think. Trying anyway."
        else "Visiting " <> chan
    joinChannel (Channel chan) Nothing
respondVisit mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 1)

cmdVisit = Command
    { cmdNames    = cmds ["visit"]
    , cmdRespond  = respondVisit
    , cmdHelp     = helps
        [ ( "visit <channel>"
          , "ask the bot to join a given channel, without storing state. So \
            \the bot won’t remember to join it next time and default settings \
            \will be used. This is useful for short tests perhaps. If you’d \
            \like the bot to become a permanent member, use !join."
          )
        ]
    , cmdExamples =
        [ "visit #freepost"
        ]
    }

respondJoin
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondJoin _mchan _nick [chan] send = do
    memb <- botMemberOf $ Channel chan
    sel <- channelSelected $ Channel chan
    send $ MsgContent $ if memb && sel
        then "I'm already a member of that channel. I think. Trying anyway."
        else "Becoming a member of " <> chan
    joinChannel (Channel chan) Nothing
    addChannel $ Channel chan
respondJoin mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 1)

cmdJoin = Command
    { cmdNames    = cmds ["join"]
    , cmdRespond  = respondJoin
    , cmdHelp     = helps
        [ ( "join <channel>"
          , "ask the bot to join a given channel, and make them a permanent \
            \member, automatically joining on bot restart. You can use !leave \
            \to make the bot leave. To invite the bot just for a temporary \
            \session, use !visit."
          )
        ]
    , cmdExamples =
        [ "join #freepost"
        ]
    }

respondLeave
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondLeave Nothing _nick [] send = send $ MsgContent "This works only in a channel."
respondLeave (Just chan) nick [] send = do
    unselectChannel chan
    send $ MsgContent $ "Leaving the channel for now. Bye! o/"
    partChannel chan $ Just $ Comment $ "Asked by " <> unNickname nick <> " to leave"
respondLeave mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 0)

cmdLeave = Command
    { cmdNames    = cmds ["leave"]
    , cmdRespond  = respondLeave
    , cmdHelp     = helps
        [ ( "leave"
          , "ask me to leave the current channel, i.e. the one in which this \
            \command has been used. If the channel was selected for \
            \auto-joining, it is now unselected."
          )
        ]
    , cmdExamples =
        [ "leave #freepost"
        ]
    }
